package me.siphone.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;
import me.siphone.adapter.TabAdapter;
import me.siphone.fragment.CallFragment;
import me.siphone.fragment.HomeFragment;
import me.siphone.fragment.MyFragment;
import org.linphone.R;

public class MainActivity extends BaseActivity {

    private ViewGroup tabHome;
    private ViewGroup tabCall;
    private ViewGroup tabMy;

    private ViewPager2 viewPager2;

    private final View.OnClickListener onTabClick =
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.tab_home:
                            Log.d("sisyphus", "切换 Tab0");
                            tabHome.setSelected(true);
                            tabCall.setSelected(false);
                            tabMy.setSelected(false);
                            viewPager2.setCurrentItem(0);
                            break;
                        case R.id.tab_call:
                            Log.d("sisyphus", "切换 Tab1");
                            tabCall.setSelected(true);
                            tabHome.setSelected(false);
                            tabMy.setSelected(false);
                            viewPager2.setCurrentItem(1);
                            break;
                        case R.id.tab_my:
                            Log.d("sisyphus", "切换 Tab2");
                            tabMy.setSelected(true);
                            tabHome.setSelected(false);
                            tabCall.setSelected(false);
                            viewPager2.setCurrentItem(2);
                            break;
                    }
                }
            };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager2 = findViewById(R.id.container);
        tabHome = findViewById(R.id.tab_home);
        tabCall = findViewById(R.id.tab_call);
        tabMy = findViewById(R.id.tab_my);

        TabAdapter tabAdapter = new TabAdapter(getSupportFragmentManager(), getLifecycle());
        tabAdapter.addFragment(new HomeFragment());
        tabAdapter.addFragment(new CallFragment());
        tabAdapter.addFragment(new MyFragment());

        viewPager2.setAdapter(tabAdapter);
        viewPager2.setOffscreenPageLimit(3);
        viewPager2.setUserInputEnabled(false);
        viewPager2.setCurrentItem(0);
        tabHome.setSelected(true);

        tabHome.setOnClickListener(onTabClick);
        tabCall.setOnClickListener(onTabClick);
        tabMy.setOnClickListener(onTabClick);
    }
}
